#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
#define PAGER "${PAGER:-more}"
#define MAXLINE 4096

void exitPrint(const char *str);
void exitPrint(const char *str){
        printf("%s\n", str);
        exit(0);
}

int main(int argc, char **argv){
    char line[MAXLINE];
    FILE *fpin, *fpout;

    if(argc != 2){
        exitPrint("argc error");
    }

    if((fpin = fopen(argv[1], "r")) == NULL){
        exitPrint("fopen error");
    }

    if((fpout = popen(PAGER, "w")) == NULL){
        exitPrint("popen error");
    }

    while(fgets(line, MAXLINE, fpin) != NULL){
        if(fputs(line, fpout) == EOF){
            exitPrint("fputs error");
        }
    }
    if(ferror(fpin)){
        exitPrint("ferror error");
    }
    if(pclose(fpout) == -1){
        exitPrint("pclose error");
    }

    exit(0);
}